//
//  TableViewShow.m
//  EquiriesProject
//
//  Created by qianfeng on 15/10/30.
//  Copyright (c) 2015年 HXH. All rights reserved.
//

#import "TableViewShow.h"
#import "smallCityModel.h"
#import "smallCtiyCellTableViewCell.h"

extern NSInteger _line;
extern NSInteger _row;

@implementation TableViewShow

{
    UIView * bgGroundView;
    UIView * showView;
    UITableView * _tableView;
    NSMutableArray * _dataArray;
    NSMutableArray * array;
    NSMutableArray * arr;
    NSMutableArray * arr01;
    NSMutableArray * citynameArray;
}



-(id)initWithFrame:(CGRect)rect
{
    self = [super initWithFrame:rect];
    if (self) {
        //在这里创建，提示View
        //获取window，只有一个window
        arr = [NSMutableArray array];
        citynameArray = [NSMutableArray array];
        for (int i=65; i<91; i++) {
            NSString * string = [NSString stringWithFormat:@"%C",(unichar)i];
            [arr addObject:string];
        }
        
        [self getData];
       
        UIWindow * window = [[UIApplication sharedApplication] keyWindow];
        bgGroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        bgGroundView.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer * tap0 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        tap0.numberOfTapsRequired = 1;
        //添加一个手势到某个视图上
        [bgGroundView addGestureRecognizer:tap0];
        
        [window addSubview:bgGroundView];
        bgGroundView.hidden = YES;
        
        showView = [[UIView alloc] initWithFrame:rect];
        showView.backgroundColor = [UIColor clearColor];
        showView.userInteractionEnabled = YES;
        [window addSubview:showView];
        
        showView.hidden = YES;
        
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [showView addSubview:_tableView];
    }
    return self;
}

-(void)getData
{
   
    [LiycRequest getAddressWithBlock:^(id obj, NSError *error) {
        if (!error) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:obj options:NSJSONReadingMutableContainers error:nil];
        
            NSDictionary * result = dict[@"result"];
            NSArray * items = result[@"items"];
            _dataArray = [NSMutableArray array];
            
            for (int i=0; i<26; i++) {
                array = [NSMutableArray array];
                for (NSDictionary * item in items) {
                    addressModel * model = [[addressModel alloc]initWithDict:item];
                    if ([model.fl isEqualToString:arr[i]]) {
                        [array addObject:item];
                    }
                }
                [_dataArray addObject:array];
            }
            for (int i=0; i<_dataArray.count; i++) {
                if ([_dataArray[i] count] == 0) {
                    [_dataArray removeObject:_dataArray[i]];
                }
            }
            
            for (NSArray * arr1 in _dataArray) {
                for (NSDictionary * dic in arr1) {
                    arr01 = [NSMutableArray array];
                    NSArray * cityarr = dic[@"cs"];
                    for (NSDictionary * value in cityarr) {
                        smallCityModel * model = [[smallCityModel alloc]initWithDic:value];
                        [arr01 addObject:model];
                        
                    }
                [citynameArray addObject:arr01];
                }
    
            }
            
            [_tableView reloadData];
            [MBProgressHUD hideHUDForView:self animated:YES];
        }else{
            [_tableView reloadData];
            NSLog(@"error = %@",error);
        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (citynameArray.count == 0) {
        return 10;
    }
    
    NSLog(@"[citynameArray[_row] count] %ld",[citynameArray[_row] count]);
    
    return [citynameArray[_row] count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    smallCtiyCellTableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"smallCtiyCellTableViewCell" owner:nil options:nil]lastObject];
    }
    smallCityModel * model;
    if (citynameArray.count == 0) {
        model = [[smallCityModel alloc]init];
    }else{
        model = citynameArray[_row][indexPath.row];
    }
    
    cell.cityLabel.text = model.cn;
    //_city = cell.cityLabel.text;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    smallCityModel * model;
    
    if (citynameArray.count == 0) {
        model = [[smallCityModel alloc]init];
    }else{
        model = citynameArray[_row][indexPath.row];
    }
    _index = indexPath.row;
    
    if ([self.delegate respondsToSelector:@selector(viewDisappear:andId:)]) {
            [self.delegate viewDisappear:model.cn andId:model.cid];
    }

}

-(void)tap
{
    [self disappearView];
}


-(void)disappearView
{
    bgGroundView.hidden = YES;
    showView.hidden = YES;
}

-(void)looklookView
{
    bgGroundView.hidden = NO;
    showView.hidden = NO;
}

@end
